<?php

namespace web\common\command;

use addons\member\model\MemberAccountModel;
use web\common\model\sys\SysParameterModel;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\input\Argument;
use think\console\Output;
use think\Exception;
use think\Db;

class Send extends Command{

    /**
     * 定义命令
     */
    protected function configure(){
        $this->setName('Send')->setDescription('RUN ETH Data RPC API');
        $this->addArgument('action', Argument::REQUIRED, "The name of the function");
        //参数2
        $this->addArgument('param', Argument::OPTIONAL, "The name of the param");
    }
    
    protected function execute(Input $input, Output $output) {
        $action = $input->getArgument('action');
        $param = $input->getArgument('param');
        if (method_exists($this,$action)) {
            $this->$action($param);
        }
        else{
            echo "method is not exists\n";
        }
    }

    public function test(){
        $userM = new MemberAccountModel();
        $count = $userM->getPushTotal(100627034);
        dump($count);

    }
    

    /**
     * 赠送所有会员任务包
     */
    public function sendAll(){
        $userM = new MemberAccountModel();
        $orderM = new \addons\equip\model\EquipOrder;
        $equipM = new \addons\equip\model\Equip;
        $eqip_data = $equipM->getDetail(1);
        $page = 0;
        $rows = 100;
        $map = [];
        $phyM = new \addons\member\model\PhysicalContribution();
        $crontabM = new \addons\config\model\Crontab;
        $list = $userM->field('id,is_auth')->where($map)->order('id asc')->limit($page*$rows,$rows)->select();
        while ($list) {
            foreach ($list as $key => $v) {
                $user_id = $v['id'];
                if($v['is_auth']==1){
                    $ex = $orderM->where(['user_id'=>$user_id,'is_send'=>1])->value('id');
                    if($ex) continue;
                }
                $maps['user_id'] = $user_id;
                $maps['status'] = 0 ;
                $ex = $orderM->where($maps)->count();

                $userM->startTrans();
                $eqip_data['user_id'] = $user_id;
                $res = $orderM->createEquip($eqip_data,1);
                if(!$res){
                    $userM->rollback();
                    echo "user_id:{$v['id']}|赠送任务包失败\n";
                    continue;
                }
                $amount = $eqip_data['exchange_amount'];//CNY
                //加入定时器,定时器发放
                $res = $crontabM->addCrontab($user_id,$amount,$amount,1,'rewardRelease');
                if(!$res){
                    $userM->rollback();
                    echo "user_id:{$v['id']}|增加奖励发放失败\n";
                    continue;
                }
                //团队业绩
                $res = $phyM->teamStrengthCal($user_id,$amount,$eqip_data['send_active'],1);
                if(!$res){
                    $userM->rollback();
                    echo "user_id:{$v['id']}|添加业绩失败\n";
                    continue;
                }
                if($ex==0){
                    //团队业绩
                    $res = $phyM->addContribution($user_id,50);
                    if(!$res){
                        $userM->rollback();
                        echo "user_id:{$v['id']}|添加贡献值失败\n";
                        continue;
                    }
                }
                $res = $userM->where('id',$user_id)->update(['is_auth'=>1,'update_time'=>NOW_DATETIME]);
                if(!$res){
                    $userM->rollback();
                    echo "user_id:{$v['id']}|添加贡献值失败\n";
                    continue;
                }
                $userM->commit();
            }
            $page++;
            $list = $userM->field('id,is_auth')->where($map)->order('id asc')->limit($page*$rows,$rows)->select();
        }
        echo "done\n";
    }
  
}